package com.marketingexpert.controller.messagemgnt;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.service.snsmgnt.AccessToken;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.snsmgnt.User;



public class GotoPostMessageController implements Controller {

	private SnsServiceFactory snsServiceFactory = null;

	private String postMessageView = null;

	private Map<String, User> users = null;

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

	public String getPostMessageView() {
		return postMessageView;
	}

	public void setPostMessageView(String postMessageView) {
		this.postMessageView = postMessageView;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		SnsService weboauth = getSnsServiceFactory().getService(SnsServiceFactory.SINA);
		
		AccessToken accessToken = (AccessToken) request.getSession().getAttribute("accessToken");

		User user = weboauth.getUserDetails(accessToken.getToken(), accessToken.getTokenSecret(), String.valueOf(accessToken.getUserId()));

		try {
			System.out.println("update weibo: " + request.getParameter("contentTxt"));		
			
			users = new HashMap<String, User>();
			users.put("user", user);
			return new ModelAndView(getPostMessageView(), users);
			
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
